package com.jinke.api.modules.ums.controller;

import com.jinke.api.modules.ums.service.UmsAdminLoginLogService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 后台用户管理
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "UmsAdminLoginLogController")
@Tag(name = "后台用户管理", description = "后台用户管理")
@RequestMapping("/admin/loginLog")
public class UmsAdminLoginLogController {

    @Autowired
    private UmsAdminLoginLogService umsAdminLoginLogService;
/*
    @ApiOperation("根据用户名或姓名分页获取用户列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<UmsAdminVO>> list(UmsAdminLoginLogListParam request) {

        Page<UmsAdmin> adminList = adminService.list(request);
        if (CollectionUtil.isEmpty(adminList.getRecords())) {
            return CommonResult.success();
        }
        CommonPage<UmsAdmin> umsAdminCommonPage = CommonPage.restPage(adminList);
        CommonPage<UmsAdminVO> umsAdminVOCommonPage = new CommonPage<>();
        BeanUtils.copyProperties(umsAdminCommonPage, umsAdminVOCommonPage);

        List<UserChannelVO> userChannelVOS = new ArrayList<>();
        List<UmsAdmin> umsAdmins = umsAdminCommonPage.getList();
        Set<Integer> userIds = umsAdmins.stream().map(UmsAdmin::getId).collect(Collectors.toSet());
        List<UserChannel> userChannels = channelUserService.list(new LambdaQueryWrapper<UserChannel>().in(UserChannel::getUserId, userIds));

        Map<Integer, UmsAdmin> sellerMap = new HashMap<>();
        Set<Integer> selleIds = umsAdmins.stream().map(UmsAdmin::getSellerId).filter(Objects::nonNull).collect(Collectors.toSet());
        if (CollectionUtil.isNotEmpty(selleIds)) {
            sellerMap = adminService.list(new LambdaQueryWrapper<UmsAdmin>()
                    .in(UmsAdmin::getId, selleIds)
                    .select(UmsAdmin::getId, UmsAdmin::getRealName, UmsAdmin::getEmail)
            ).stream().collect(Collectors.toMap(UmsAdmin::getId, Function.identity()));
        }

        Map<Integer, List<UserChannelVO>> userChannelMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(userChannels)) {
            Set<Integer> channelIds = userChannels.stream().map(UserChannel::getChannelId).collect(Collectors.toSet());
            List<Channel> channels = channelService.list(new LambdaQueryWrapper<Channel>().in(Channel::getId, channelIds));
            Map<Integer, Channel> channelMap = channels.stream()
                    .collect(Collectors.toMap(Channel::getId, Function.identity()));

            Set<Integer> channelPlatformIds = userChannels.stream().map(UserChannel::getChannelPlatformId).collect(Collectors.toSet());
            Map<Integer, String> channelPlatformMap = channelPlatformService.list(new LambdaQueryWrapper<ChannelPlatform>()
                            .in(ChannelPlatform::getId, channelPlatformIds))
                    .stream().collect(Collectors.toMap(ChannelPlatform::getId, ChannelPlatform::getChannelPlatformName));

            Set<Integer> channelCarrierIds = userChannels.stream().map(UserChannel::getChannelCarrierId).collect(Collectors.toSet());
            Map<Integer, String> channelCarrierMap = channelCarrierService.list(new LambdaQueryWrapper<ChannelCarrier>()
                            .in(ChannelCarrier::getId, channelCarrierIds))
                    .stream().collect(Collectors.toMap(ChannelCarrier::getId, ChannelCarrier::getChannelCarrierName));

            for (UserChannel userChannel : userChannels) {
                UserChannelVO userChannelVO = new UserChannelVO();
                BeanUtils.copyProperties(userChannel, userChannelVO);
                Channel channel = channelMap.get(userChannelVO.getChannelId());
                if (channel != null) {
                    userChannelVO.setChannelName(channel.getChannelName());
                    userChannelVO.setChannelPlatFormName(channelPlatformMap.get(channel.getChannelPlatformId()));
                    userChannelVO.setChannelCarrierName(channelCarrierMap.get(channel.getChannelCarrierId()));
                }

                userChannelVOS.add(userChannelVO);
            }
            userChannelMap = userChannelVOS.stream().collect(Collectors.groupingBy(UserChannelVO::getUserId));
        }

        Map<Integer, List<UserChannelVO>> finalUserChannelMap = userChannelMap;
        Map<Integer, UmsAdmin> finalSellerMap = sellerMap;
        List<UmsAdminVO> umsAdminVOS = umsAdmins.stream().map(x -> {
            UmsAdminVO umsAdminVO = new UmsAdminVO();
            umsAdminVO.setUserChannels(finalUserChannelMap.get(x.getId()));
            BeanUtils.copyProperties(x, umsAdminVO);

            List<UmsRole> roleList = adminService.getRoleList(x.getId());
            if (CollUtil.isNotEmpty(roleList)) {
                List<String> roles = roleList.stream().map(UmsRole::getName).collect(Collectors.toList());
                umsAdminVO.setRoles(roles);
            }

            Integer sellerId = x.getSellerId();
            if (sellerId != null) {
                UmsAdmin umsAdmin = finalSellerMap.get(sellerId);
                if (umsAdmin != null) {
                    umsAdminVO.setSeller(umsAdmin);
                }
            }

            return umsAdminVO;
        }).collect(Collectors.toList());
        umsAdminVOCommonPage.setList(umsAdminVOS);
        return CommonResult.success(umsAdminVOCommonPage);
    }

*/


}
